<?php


namespace app\api\controller;

/**
 * Class 订单详情
 * @package app\api\controller
 */
class Orderinfo extends \app\common\controller\Api
{
    protected $noNeedRight = ['*'];
    /**
     * 获取订单信息 -点击卖出
     */
    public function get_order_info(){
        $buy_id = input('buy_id') ? input('buy_id') :$this->error('未指定卖出订单');
        $buy = db('buy')->find($buy_id);
        if (!$buy || $buy['status'] != 1 ){
            $this->error('改买入请求已被他人匹配或取消');
        }
        $user = $this->auth->getUser();
        if ($user->is_truename == 1){
            $this->error('请先通过实名验证');
        }
        $data = [];
        $data['pay_images'] = false;
        if ($user->alipay_image){
            $data['pay_images'][] = $user->alipay_image;
        }
        if ($user->wechat_image){
            $data['pay_images'][] = $user->wechat_image;
        }
        if (!$data['pay_images']){
            $this->error('请先设置收款码');
        }
        $data['name'] = db('user_realname')
            ->where('user_id',$buy['user_id'])
            ->where('status','2')
            ->value('truename');
        $data['mobile'] = db('user')->where('id',$buy['user_id'])->value('mobile');
        if ($buy['type'] == 1){
            //CB
            $data['type_text'] = 'CB';
            $price = db('cb_price')->order('id DESC')->find();

        }else{
            //cc
            $data['type_text'] = 'CC';
            $price = db('cc_price')->order('id DESC')->find();
        }

        $to_cny = db('config')->where('name','to_cny')->value('value');
        $price = round($price['price'] * $to_cny /100,2);
        $data['one_price'] = $buy['one_price'] ?? $price;
        $data['num'] = $buy['num'];
        $data['all_price'] = round($data['one_price'] * $buy['num'],2);
        $data['nickname'] = $user->nickname;
        $data['myname'] = db('user_realname')
            ->where('user_id',$user->id)
            ->where('status','2')
            ->value('truename');
        $data['mymobile'] = $user->mobile;

        $this->success('ok',$data);
    }
    /**
     * 获取订单信息 -委托管理交易中
     */
    public function weituo_ing(){
        $order_id = input('order_id') ? input('order_id') :$this->error('未指定订单');
        $order = model('order')
            ->with('buyuser')
            ->with('selluser')
            ->where('id',$order_id)
            ->find();
        if (!$order){
            $this->error('订单指定错误');
        }
        $user = $this->auth->getUser();
        if ($user->is_truename == 1){
            $this->error('请先通过实名验证');
        }
        $data = [];

        $data['order']['type'] = $order->type;
        if ($order->type == 1){
            //CB
            $data['order']['type_text'] = 'CB';
        }else{
            //cc
            $data['order']['type_text'] = 'CC';
        }
        $data['order']['one_price'] = $order->one_price;//单价
        $data['order']['num'] = $order->num;//数量
        $data['order']['createtime'] = date('Y-m-d H:i:s',$order->createtime);//匹配时间
        $data['order']['overtime'] = changeTimeTypetrue($order->overtime - time());//剩余时间
        $data['order']['overtime_num'] = changeTimeType($order->overtime - time()) * 1000;//剩余时间
        $data['order']['order_price'] = round($order->num * $order->one_price,2);//总价
        $data['order']['pay_images'] = explode(',',$order->pay_images);

        if ($order->sell_user_id == $user->id){

            $data['sell']['title'] = '买家信息';
            $buyuser = db('user')->find($order->buy_user_id);//卖家信息
            $data['sell']['pay_images'] = false;
            $data['sell']['mobile'] = $buyuser['mobile'];
            $data['sell']['nickname'] = $buyuser['nickname'];
            $data['sell']['name'] = db('user_realname')
                ->where('user_id',$buyuser['id'])
                ->where('status','2')
                ->value('truename');

        }else{
            $data['sell']['title'] = '卖家信息';
            $selluser = db('user')->find($order->sell_user_id);//卖家信息
            $data['sell']['pay_images'] = [$selluser['alipay_image'],$selluser['wechat_image']];
            $data['sell']['nickname'] = $selluser['nickname'];
            $data['sell']['name'] = db('user_realname')
                ->where('user_id',$selluser['id'])
                ->where('status','2')
                ->value('truename');
            $data['sell']['mobile'] = $selluser['mobile'];
            $data['sell']['bank_num'] = $selluser['bank_num'];//银行卡号
            $data['sell']['bank_name'] = $selluser['bank_name'];//开户行
        }

        if ($order['status'] == 1){
            //待付款
            if($order['buy_user_id'] == $user->id){
                $data['operate']['msg'] = '我已付款';
                $data['operate']['type'] = 1;
                $data['appeal'] = false;
            }else{
                $data['operate']['msg'] = '等待买家付款';
                $data['operate']['type'] = 2;
                $data['appeal'] = false;
            }

        }elseif($order['status'] == 2){
            //代收款
            if($order['buy_user_id'] == $user->id){
                $data['operate']['msg'] = '等待卖家收款';
                $data['operate']['type'] = 3;
                $data['appeal'] = true;
            }else{
                $data['operate']['msg'] = '确认收款';
                $data['operate']['type'] = 4;
                $data['appeal'] = true;
            }

        }elseif($order['status'] == 4){
            //买家申诉
            if($order['buy_user_id'] == $user->id){
                $data['operate']['msg'] = '申诉中';
                $data['operate']['type'] = 5;
            }else{
                $data['operate']['msg'] = '卖家申诉中';
                $data['operate']['type'] = 6;
            }
            $data['appeal'] = false;
        } elseif($order['status'] == 5){
            //卖家申诉
            if($order['buy_user_id'] == $user->id){
                $data['operate']['msg'] = '卖家申诉中';
                $data['operate']['type'] = 7;
            }else{
                $data['operate']['msg'] = '申诉中';
                $data['operate']['type'] = 8;
            }
            $data['appeal'] = false;
        }else{
            //没其他操作
            $data['operate']['msg'] = '';
            $data['operate']['type'] = 0;
            $data['appeal'] = false;
        }
        $this->success('ok',$data);
    }

    /**
     * 委托管理 -  历史订单
     */
    public function history_order(){
        $order_id = input('order_id') ? input('order_id') :$this->error('未指定订单');
        $order = model('order')
            ->with('buyuser')
            ->with('selluser')
            ->where('id',$order_id)
            ->find();
        if (!$order){
            $this->error('订单指定错误');
        }
        $user = $this->auth->getUser();
        if ($user->is_truename == 1){
            $this->error('请先通过实名验证');
        }
        $data = [];

        $data['order']['type'] = $order->type;
        if ($order->type == 1){
            //CB
            $data['order']['type_text'] = 'CB';
        }else{
            //cc
            $data['order']['type_text'] = 'CC';
        }
        $data['order']['one_price'] = $order->one_price;
        $data['order']['num'] = $order->num;
        $data['order']['createtime'] = date('Y-m-d H:i:s',$order->createtime);
        $data['order']['all_price'] = round($order->one_price * $order->num,2);
        $data['order']['pay_images'] = explode(',',$order->pay_images);

        if ($order->sell_user_id == $user->id){
            //自己是卖家  看买家信息
            $data['user']['title'] = '买家信息';
            $selluser = db('user')->find($order->buy_user_id);//卖家信息
//            $data['user']['pay_images'] = [$selluser['alipay_image'],$selluser['wechat_image']];
            $data['user']['pay_images'] = false;
            $data['user']['mobile'] = $selluser['mobile'];
            $data['user']['nickname'] = $selluser['nickname'];
            $data['user']['name'] = db('user_realname')
                ->where('user_id',$order->sell_user_id)
                ->where('status','2')
                ->value('truename');
            $data['user']['bank_num'] = $selluser['bank_num'];//银行卡号
            $data['user']['bank_name'] = $selluser['bank_name'];//开户行
        }else{
            //自己是买家  看卖家信息
            $data['user']['title'] = '卖家信息';
            $buyuser = db('user')->find($order->sell_user_id);//买家信息
            $data['user']['pay_images'] = [$buyuser['alipay_image'],$buyuser['wechat_image']];
            $data['user']['nickname'] = $buyuser['nickname'];
            $data['user']['name'] = db('user_realname')
                ->where('user_id',$buyuser['id'])
                ->where('status','2')
                ->value('truename');
            $data['user']['mobile'] = $buyuser['mobile'];
            $data['user']['bank_num'] = $buyuser['bank_num'];//银行卡号
            $data['user']['bank_name'] = $buyuser['bank_name'];//开户行
        }
        $this->success('ok',$data);
    }
}